package com.example.barcodeapp.ui.own.fragment;

import android.util.Log;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.barcodeapp.R;
import com.example.barcodeapp.adapter.FragmentAdapter;
import com.example.barcodeapp.base.BaseFragment;
import com.example.barcodeapp.common.Constants;
import com.example.barcodeapp.interfaces.IBasePersenter;
import com.example.barcodeapp.ui.home.AdapterRili;
import com.example.barcodeapp.ui.own.bean.KeMuBean;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class FragmentSuYang extends BaseFragment {


    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;



    private ArrayList<Fragment> fragments;
    private FragmentAdapter fragmentAdapter;//categoryList
    @Override
    protected int getLayout() {
        return R.layout.fragment_su_yang;
    }

    @Override
    protected void initView() {

        Http();


    }

    private void Http() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL("http://new.jifengyinyue.com/api/index/coursecate");

                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    int responseCode = urlConnection.getResponseCode();
                    if (responseCode == HttpURLConnection.HTTP_OK) {
                        InputStream inputStream = urlConnection.getInputStream();
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                        String line = null;
                        StringBuffer stringBuffer = new StringBuffer();
                        while ((line = bufferedReader.readLine()) != null) {
                            stringBuffer.append(line);
                        }
                        String json = stringBuffer.toString();
                        final Gson gson = new Gson();
                        final KeMuBean fengzhuanglei = gson.fromJson(json, KeMuBean.class);
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                List<KeMuBean.DataEntity> dataEntities;
                                dataEntities=fengzhuanglei.getData();
                                fragments = new ArrayList<>();
                                for (int i = 0; i < dataEntities.size(); i++) {
                                    fragments.add(new FragmentQiTa());
                                }
                                fragmentAdapter = new FragmentAdapter(getChildFragmentManager(), fragments);
                                viewPager.setAdapter(fragmentAdapter);
                                tabLayout.setupWithViewPager(viewPager);
                                tabLayout.setTabIconTint(null);//显示图片
                                tabLayout.setSelectedTabIndicatorHeight(0);
                                for (int i = 0; i < dataEntities.size(); i++) {
                                    tabLayout.getTabAt(i).setText(dataEntities.get(i).getCoursecate());
                                }

                                tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                                    @Override
                                    public void onTabSelected(TabLayout.Tab tab) {

                                        Constants.kemuid=dataEntities.get(tab.getPosition()).getId();

                                    }

                                    @Override
                                    public void onTabUnselected(TabLayout.Tab tab) {

                                    }

                                    @Override
                                    public void onTabReselected(TabLayout.Tab tab) {

                                    }
                                });







                            }
                        });

                    }

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    protected IBasePersenter initPersenter() {
        return null;
    }

    @Override
    protected void initData() {

    }


}